package com.hollycrm.ahunicom.sheet.business.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class AjaxServlet extends HttpServlet {

    //Initialize global variables
    public void init() throws ServletException {
       
    }

    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
     
    }

    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	try{
      PrintWriter out = response.getWriter();  
	  String oldurl=request.getParameter("url");
	  String url=oldurl.replace('#','&');
	  java.net.URL _url = new java.net.URL(url);   
      java.net.URLConnection urlcon = _url.openConnection();   
      java.io.InputStream is = urlcon.getInputStream();   
      java.io.BufferedReader buffer = new java.io.BufferedReader(new java.io.InputStreamReader(is));   
      StringBuffer bs = new StringBuffer();   
      String lineStr = null;   
      while ((lineStr = buffer.readLine()) != null) {   
       // String stri = java.net.URLDecoder.decode(lineStr, "UTF-8");   
       // bs.append(stri).append("\n");   
    	  bs.append(lineStr).append("\n");   
    }   
//    if (bs.toString().indexOf("<?xml version=") != -1) {//if XML file, for AJAX   
//    	response.setContentType("text/xml; charset=UTF-8");   
//        response.setHeader("Cache-Control", "no-cache");   
//        response.getWriter().write(bs.toString().trim());
//        response.flushBuffer(); 
//        //out.println(bs.toString());  
//        //out.flush();
//        //out.write(bs.toString().trim());
//    } else  {
//       // out.println(bs.toString());  
//    	// out.write(bs.toString().trim());
//    	 response.getWriter().write(bs.toString().trim());
//         response.flushBuffer(); 
//    }
      // response.setContentType("text/xml; charset=UTF-8");   
       response.setHeader("Cache-Control", "no-cache");  
       System.out.println("====="+bs.toString().trim()+"==========");
       response.getWriter().print(bs.toString().trim());
       response.flushBuffer(); 
//      //out.flush();
//      //out.write(bs.toString().trim());
//  }
    	}catch (Exception ex){
    		response.setStatus(500);
    	     response.flushBuffer(); 
    	}
}   

   

    //Clean up resources
    public void destroy() {
    }

}